//
//  RwdPictureView.swift
//  Weibo_rwd
//
//  Created by 任文迪 on 16/7/15.
//  Copyright © 2016年 spaceX. All rights reserved.
//

import UIKit
//重用标记
let PictureViewCellIdentifier = "PictureViewCellIdentifier"
//每项的间距
private let itemMargin: CGFloat = 5
//itemwidth
let itemWidth = (UIScreen.mainScreen().bounds.size.width - 2 * itemMargin - 2 * margin) / 3

class RwdPictureView: UICollectionView ,UICollectionViewDataSource{
    
    //引入数据模型
    var pictureUrls:[RwdStatusImageModel]?{
        
        didSet{
            let size = calPictureSize(pictureUrls?.count ?? 0)
            
            print(pictureUrls?.count)
            
            self.snp_updateConstraints { (make) -> Void in
                make.size.equalTo(size)
            }
            
            self.reloadData()
        }
        
    }
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = CGSize(width: itemWidth, height: itemWidth)
        layout.minimumInteritemSpacing = itemMargin
        layout.minimumLineSpacing = itemMargin
        super.init(frame: frame, collectionViewLayout: layout)
        backgroundColor = UIColor.blackColor()
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
      private func setUpUI() {
        registerClass(costomCell.self, forCellWithReuseIdentifier: PictureViewCellIdentifier)
        //  设置数据源代理
        dataSource = self
    
    }
    
    func calPictureSize(count:Int)->CGSize {
        //列数,如果图片数目大于3 那么就三列 如果小于三就返回count
        let cols = count > 3 ? 3:count
        //行数
        let cows = (count - 1) / 3 + 1
        //  计算宽度
        let width = CGFloat(cols) * itemWidth + CGFloat(cols - 1) * itemMargin
        //  计算高度
        let height = CGFloat(cows) * itemWidth + CGFloat(cows - 1) * itemMargin
        //cell的大小
        let Size = CGSize(width: width, height: height)
        
        return Size
    }
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return pictureUrls?.count ?? 0
        
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(PictureViewCellIdentifier, forIndexPath: indexPath) as!costomCell
        
//        cell.backgroundColor = UIColor.grayColor()
        
        cell.pictrues = pictureUrls![indexPath.item]
        
        return cell
        
    }
    
    

}


class costomCell: UICollectionViewCell {
    
    var pictrues:RwdStatusImageModel? {
        
        didSet{
            if let imageUrl = pictrues?.thumbnail_pic {
                
                imageView.sd_setImageWithURL(NSURL(string: imageUrl), placeholderImage: UIImage(named: "timeline_image_placeholder"))
                
            }
        }
        
    }
    //设置一个imageview放在uicollectionviewcell上
    private var imageView : UIImageView = {
        
       let image = UIImageView(image: UIImage(named: "timeline_image_placeholder"))
        
        return image
        
    }()
    //重写init 方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setUpUI() {
        
        contentView.addSubview(imageView)
        imageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
        
    }
    
    
    
}



